import { wrong_check, no_del, cmd_uid, for_obj, urlMd, split_md, obj_empty, cline, is_empty } from '../../nui';
import { loc } from '../../local';

const load_cfg = (data, setUid, setMain) => {
    if (obj_empty(data)) {
        setMain(null);
        if (setUid) {
            setUid(null);
        }
        return;
    }

    if (setUid) {
        setUid(data.uid);
    }

    const [ekey, edes] = split_md(data, "edes", "ekey");
    if (is_empty(ekey) || is_empty(edes)) {
        setMain(null);
        return;
    }

    const ret = ekey.map((d, i) => {
        return {index: i + 1, sn: d, des: edes[i]};
    });

    setMain(ret);
};

const loadCfg = (data, uid, umd, hcmd, ck, mod) => {
    umd.startWait();

    const check = {
        col: "pos,key,des",
        num: ["pos"]
    };

    let val = [];
    for (let i = 0; i < data.length; i++) {
        const d = data[i];

        const ret = wrong_check(d, loc("cspz"), check);
        if (ret) {
            umd.setWrong(ret);
            return;
        }

        let tmp = {
            pos: d["pos"],
            key: d["key"],
            des: d["des"],
        };

        val.push(tmp);
    }

    for (let i = 0; i < ck.length; i++) {
        const v = ck[i];
        const f = val.find(d => d["key"] === v);
        if (!f) {
            umd.setWrong(`${loc("qszd")}: ${v}`);
            return;
        }
    }

    val = val.sort((a, b) => parseInt(a.pos, 10) > parseInt(b.pos, 10));

    let pos = [];
    let key = [];
    let des = [];
    let type = [];

    val.forEach(d => {
        pos.push(d["pos"]);
        key.push(d["key"]);
        des.push(d["des"]);

        if (ck) {
            type.push(d["type"]);
        }
    });

    for (let i = 0; i < pos.length; i++) {
        const d = parseInt(pos[i])
        if (d !== i + 1) {
            umd.setWrong(loc("xhcw") + d);
            return;
        }
    }

    const ekey = key.join(",");
    const edes = des.join(",");
    const keyv = type.join(",");

    const cmd = {};

    if (uid) {
        const tmp = [
            {k: "ekey", v: ekey},
            {k: "edes", v: edes},
        ];

        if (ck) {
            tmp.push({k: "keyv", v: keyv});
        }

        cmd["m"] = [{
            u: uid,
            v: tmp,
        }];
    }
    else {
        const tmp = [
            {k: "sn", v: mod},
            {k: "ekey", v: ekey},
            {k: "edes", v: edes},
        ];

        if (ck) {
            tmp.push({k: "keyv", v: keyv});
        }

        cmd["a"] = [{
            u: cline(),
            n: "cfg",
            v: [tmp],
        }];
    }

    urlMd(null, cmd,
        r => {
            hcmd();
            umd.endWait();
        },
        e => umd.setWrong(loc("szsb"))
    );
};

const qc_cfg = (cfg) => {
    if (obj_empty(cfg)) {
        return null;
    }

    const [ekey, edes] = split_md(cfg, "edes", "ekey");
    if (is_empty(ekey) || is_empty(edes)) {
        return null;
    }

    const ret = {};
    ekey.forEach((d, i) => ret[d] = edes[i]);

    return ret;
};

const qc_lst = (cfg) => {
    const ekey = qc_cfg(cfg);
    if (obj_empty(ekey)) {
        return null;
    }

    let obj = [];
    for_obj(ekey, (k, v) => obj.push(k));

    return obj;
};

const rmQc = (nduid, uid, umd, hcmd, mod) => {
    if (no_del()) {
        return;
    }

    umd.startWait();

    urlMd(null,
        {
            r: [{
                u: nduid,
                v: [{k: mod, u: uid}]
            }]
        },
        r => {
            hcmd();
            umd.endWait();
        }, e => umd.setWrong(loc("xgsb")));
};

export {qc_cfg, qc_lst, rmQc, loadCfg, load_cfg};
